package com.wy.projectssj.dao.impl;

import com.wy.projectssj.dao.SysLogDao;
import com.wy.projectssj.domain.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: wy
 * @Date:2019/8/29 11:24
 */
@Repository
public class SysLogDaoImpl implements SysLogDao {
     @Autowired
     private JdbcTemplate jt;
    @Override
    public int findAll(){

        String sql = "select count(*) from syslog";
        int count = -1;
        try {
            count = jt.queryForObject(sql,Integer.class);
        } catch (DataAccessException e) {
        }
        return count;
    }


    @Override
    public List<SysLog> findByCondition(Integer start, Integer rows) {
         String sql="select * from syslog order by visitTime desc limit ?,? ";
        List<SysLog> list=null;
        try {
            list = jt.query(sql, new BeanPropertyRowMapper<>(SysLog.class), start, rows);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int save(SysLog sysLog){
         int result=-1;
          String sql="insert into syslog values(?,?,?,?,?,?,?)";
        try {
            result = jt.update(sql, sysLog.getId(),
                    sysLog.getVisitTime(),
                    sysLog.getUsername(),
                    sysLog.getIp(),
                    sysLog.getUrl(),
                    sysLog.getExecutionTime(),
                    sysLog.getMethod());
        } catch (DataAccessException e) {

        }
             return  result;
    }
}
